<?php
/**
 * 后台公共控制器
 * @author cjli
 *
 */
class AdminAction extends Action
{
    public function _initialize()
    {
    	L(include(LANG_PATH.C('DEFAULT_LANG').'/common.php'));
		  header("Content-Type:text/html; charset=utf-8");
		   if(MODULE_NAME == 'Public'){
		   	    return true;
		   } else {
		   	    if( ACTION_NAME != "swfupload_ajax" &&  ACTION_NAME != "order_print_figure_god"){
					if(!isset($_SESSION['admin_user_info']) || empty($_SESSION['admin_user_info'])){
							$this->redirect(U('public/login'));
					}
				}
			}
			//特殊处理
			/*if( ACTION_NAME != "swfupload_ajax" &&  ACTION_NAME != "order_print_figure_god"){
			    self::check_priv();
		    }*/
			$languageList = SiteModel::getLanguageList();
			$this->assign('languageList', $languageList);
			
//			// 加载网站配置文件
//      if(is_file(ROOT_PATH.'/data/site_config.php')){
//      	C(include ROOT_PATH.'/data/site_config.php');
//        C('DEFAULT_THEME', '');
//       }

			//保存配置值
			$configList = SiteModel::getShopConfigList();
			unset($configList['DEFAULT_THEME']);
			C($configList);
		}
		
		/**
		 * 权限判断
		 */
	final public function check_priv()
	{
		$action = ACTION_NAME;
		if((isset($_SESSION['admin_user_info']) && $_SESSION['admin_user_info']['roleid'] == 1) || $action=='index') {
			return true;
		}
		
		if(preg_match('/public_/i',ACTION_NAME) || preg_match('/_ajax/i',ACTION_NAME) || preg_match('/insert_/i',ACTION_NAME) || ACTION_NAME=='upload') return true;
		if(preg_match('/ajax_([a-z]+)_/i',ACTION_NAME,$_match)) {
			$action = $_match[1];
		}

		$privdb = D('admin_role_priv');
		$r = $privdb->where(array('c'=>strtolower(MODULE_NAME),'a'=>$action,'roleid'=>$_SESSION['admin_user_info']['roleid']))->find();
		if(!$r) $this->error('您没有权限操作权限,请联系管理员','goback');
	}
	/**
	 * 按父ID查找菜单子项
	 * @param integer $parentid   父菜单ID  
	 * @param integer $with_self  是否包括他自己
	 */
	final public static function menu($parentid, $with_self = 0)
	{
		$parentid = intval($parentid);
		$menudb = D('menu');
		$where = array('parentid'=>$parentid,'display'=>1);
		$menudb->limit(1000);
		$menudb->order('listorder DESC');
		$menudb->where($where);
		$result = $menudb->select();
		if($with_self) {
			$result2[] = $menudb->where(array('id'=>$parentid))->find();
			$result = array_merge($result2,$result);
		}
		//权限检查
		if($_SESSION['admin_user_info']['roleid'] == 1) return $result;
		$array = array();
		$privdb = D('admin_role_priv');
		foreach($result as $v) {
			$action = $v['a'];
			if(preg_match('/^public_/',$action)) {
				$array[] = $v;
			} else {
				if(preg_match('/^ajax_([a-z]+)_/',$action,$_match)) $action = $_match[1];
				$r = $privdb->where(array('c'=>$v['c'],'a'=>$action,'roleid'=>$_SESSION['admin_user_info']['roleid']))->find();
				if($r) $array[] = $v;
			}
		}
		return $array;
	}
	
	/**
	 * 当前位置
	 * 
	 * @param $id 菜单id
	 */
	final public static function current_pos($id)
	{
		$menudb = D('menu');
		$r = $menudb->field('id,name,parentid')->where(array('id'=>$id))->find();
		$str = '';
		if($r['parentid']) {
			$str = self::current_pos($r['parentid']);
		}
		return $str.$r['name'].' > ';
	}
	
	/**
	 * (non-PHPdoc)
	 * @see Action::error()
	 * @param $dialog dialog弹窗
	 */
	protected function error($message, $jumpUrl = '', $dialog = '', $ajax = false)
	{
		$this->assign('dialog', $dialog);
		parent::error($message, $jumpUrl, $ajax);
	}
	/**
	 * (non-PHPdoc)
	 * @see Action::success()
	 * @param $dialog dialog弹窗
	 */
	protected function success($message, $jumpUrl = '', $dialog = '', $ajax = false)
	{
		$this->assign('dialog', $dialog);
		parent::success($message, $jumpUrl, $ajax);
	}
	
	/**
	 * 单个附件上传
	 * @param string $subDir 子目录名称当$subType为custom时有效
	 * @param string $subType 子目录创建方式 可以使用hash date custom
	 * @param string $savePath 设置附件上传目录
	 * @param array $allowExts 设置附件上传类型
	 */
	final public static function upload($subDir = '', $subType = 'custom', $allowExts = array())
	{
		import('ORG.Net.UploadFile');
		$upload = new UploadFile();
		$upload->allowExts  = $allowExts ? $allowExts : array('jpg', 'gif', 'png', 'jpeg');
		$upload->savePath =  '.'.UPLOAD_PATH;
		$upload->autoSub = true;
		$upload->subType = $subType ? $subType : 'custom';
		$upload->subDir = $subDir.'/';
		if(!is_dir(UPLOAD_PATH . $subDir)){
			mkdir(UPLOAD_PATH . $subDir);
		}
		if(!$upload->upload()) {// 上传错误提示错误信息
			return $upload->getErrorMsg();
		}else{// 上传成功 获取上传文件信息
			$info = $upload->getUploadFileInfo();
			$fileArray = array();
			foreach ($info as $sinfo){
				$fileArray[$sinfo['key']] = $sinfo;
			}
			return $fileArray;
		}
	}
}
?>